<?php
include_once ("../Login/databaseconnection.php");
$type=$_POST['ResName'];
$resultr= mysql_query("SELECT * FROM resource Where Resource_Type='$type'");
if($row = mysql_fetch_array($resultr))
{
$Resource_Id=$row['Resource_Id'];
}

$Serial_Number=$_POST['Serial_Number'];
if($type!=""&&$Lab_Id!="Select One")
	   {
  $result=mysql_query("SELECT * FROM resource_details WHERE Resource_Id='$Resource_Id' AND Serial_Number='$Serial_Number'");
  if($result)
  {
  if($row=mysql_fetch_array($result))
  {
  $Resource_Id=$row['Resource_Id'];
     $Resource_Type=$type;
$Lab_Id=$row['Lab_Id'];
   $Serial_Number=$row['Serial_Number'];
 $Functioning=$row['Functioning'];
    $Brand_Name=$row['Brand_Name'];
    $Date_Of_Supplied=$row['Date_Of_Supplied'];
    $Name_Of_Supplier=$row['Name_Of_Supplier'];
    $Funding_Source=$row['Funding_Source'];
	$School_Id=$row['School_Id'];
  }
  		  if(mysql_num_rows($result)==0)
   {
	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   else
   {
   if($_SESSION['Access_Type']=="Administrator")
   {    
	{
   	 $del=mysql_query("DELETE FROM resource_details WHERE Resource_Id='$Resource_Id' AND Serial_Number='$Serial_Number'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }
  if(mysql_affected_rows()==0)
  {
 $_SESSION['error']="Sorry unable to delete this record!";
   $Resource_Id='';
$Lab_Id='';
   $Serial_Number='';
 $Functioning='';
    $Brand_Name='';
    $Date_Of_Supplied='';
    $Name_Of_Supplier='';
    $Funding_Source='';
	$School_Id='';
  }
  }
   } 
   else if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if(mysql_num_rows($get_Sch)!=0)
{
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
	$get_LaSNo=mysql_query("SELECT * FROM resource_details WHERE Serial_Number='$Serial_Number'");
	
	if($row_LaSNo=mysql_fetch_array($get_LaSNo))
{
	 $DSchool_Id=$row_LaSNo['School_Id'];
	 if($DSchool_Id==$Own_School_Id)
	{
	$Accessed_By=$_SESSION['NIC_Number'];
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
	if($row=mysql_fetch_array($result))
   {
   $Resource_Id=$row['Resource_Id'];
   $Lab_Id=$row['Lab_Id'];
   $Serial_Number=$row['Serial_Number'];
   $Functioning=$row['Functioning'];
   $Brand_Name=$row['Brand_Name'];
   $Date_Of_Supplied=$row['Date_Of_Supplied'];
   $Name_Of_Supplier=$row['Name_Of_Supplier'];
   $Funding_Source=$row['Funding_Source'];
   $School_Id=$row['School_Id'];
   $Last_Updated_Date=$row['Last_Updated_Date'];  
   }
  	 $sql=mysql_query("INSERT INTO deleted_resource_details(Resource_Id,Lab_Id,Serial_Number,Functioning,Brand_Name,Date_Of_Supplied,Name_Of_Supplier,Funding_Source,School_Id,Deleted_date,Accessed_By,Last_Updated_Date) VALUES('$Resource_Id','$Lab_Id','$Serial_Number','$Functioning','$Brand_Name','$Date_Of_Supplied','$Name_Of_Supplier','$Funding_Source','$School_Id','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql)
{
    $del=mysql_query("DELETE FROM resource_details WHERE Resource_Id='$Resource_Id' AND Serial_Number='$Serial_Number'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
  }
  if(mysql_affected_rows()==0)
  {
    $Resource_Id='';
$Lab_Id='';
   $Serial_Number='';
 $Functioning='';
    $Brand_Name='';
    $Date_Of_Supplied='';
    $Name_Of_Supplier='';
    $Funding_Source='';
	$School_Id='';
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_resource_details WHERE Resource_Id='$Resource_Id' AND Deleted_Date='$t_d' AND Serial_Number='$Serial_Number'");
  }
  }
     if(!$sql)
{
  $Resource_Id='';
$Lab_Id='';
   $Serial_Number='';
 $Functioning='';
    $Brand_Name='';
    $Date_Of_Supplied='';
    $Name_Of_Supplier='';
    $Funding_Source='';
	$School_Id='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
  }
  else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  }
  
   }
   }
   }
  else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
  }
  }
   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
  }
  }
  }
  if($type==""||$Lab_Id=="Select One")
	   {
	   $_SESSION['errorin']="Please select the Resource type and Serial number!";
	   }
?>

